package Vistas;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SpinnerListModel;

import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;

import Modelos.Categoria;
import Modelos.Ingrediente;
import Modelos.IngredienteProducto;
import Modelos.Producto;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class FrmProducto extends javax.swing.JFrame {
	private JLabel LblCodigo;
	private JTextField TxtCodigo;
	private JLabel LblNombre;
	private JLabel LblPrecio;
	private JTextField TxtCantidad;
	private JLabel LblCantidad;
	private JButton BtnEliminar;
	private JButton BtnAgregar;
	private JButton BtnGuardar;
	private JTable Jtable1;
	private JComboBox CmbIngrediente;
	private JLabel LblIngrediente;
	private JComboBox CmbCategoria;
	private JLabel LblCategoria;
	private JTextField TxtPrecio;
	private JTextField TxtNombre;
	private DefaultTableModel Jtable1Model;
	private Vector<IngredienteProducto> Detalle;

	/**
	* Auto-generated main method to display this JFrame
	*/
	
	
	public void addListener(ActionListener actionlistener){
		BtnAgregar.addActionListener(actionlistener);
		BtnEliminar.addActionListener(actionlistener);
		BtnGuardar.addActionListener(actionlistener);
		
	}
	
	public FrmProducto() {
		super();
		initGUI();
		TxtCodigo.setEnabled(false);
		Detalle = new Vector<IngredienteProducto>();
	}
	
	public void AgregarFila(){
		Detalle.add(new IngredienteProducto(Integer.valueOf(TxtCodigo.getText()), CmbIngrediente.getSelectedIndex()+1,Double.valueOf(TxtCantidad.getText())));
		System.out.println("codigo: "+TxtCodigo.getText()+ " ingrediente: "+ (CmbIngrediente.getSelectedIndex()+1) + " cantidad: " + TxtCantidad.getText());
		Vector fila = new Vector();
		fila.add(CmbIngrediente.getSelectedItem());
		fila.add(TxtCantidad.getText());
		Jtable1Model.addRow(fila);
		
		TxtNombre.setEnabled(false);
		TxtPrecio.setEnabled(false);
		CmbCategoria.setEnabled(false);
		TxtCantidad.setText("");
	}
	
	
	
	
	
	public Vector<IngredienteProducto> getDetalle() {
		return Detalle;
	}

	public Producto getProducto(){
		return new Producto(Integer.valueOf(TxtCodigo.getText()), TxtNombre.getText(), Double.valueOf(TxtPrecio.getText()), CmbCategoria.getSelectedIndex()+1);
	}
	
	public void RemoverFila(){
		
		if(Jtable1.getSelectedRow()!=-1){
			Detalle.remove(Jtable1.getSelectedRow());
			System.out.println("selected " +Jtable1.getSelectedRow() );
			Jtable1Model.removeRow(Jtable1.getSelectedRow());
			//Detalle.removeElementAt(Jtable1.getSelectedRow());	
			
		}
		else
			JOptionPane.showMessageDialog(rootPane,"No ha selecciona nada");
		System.out.println("talla de detalle: "+ Detalle.size());
	}
	
	public void AgregarComboCategoria(Vector<Categoria> vector){
		DefaultComboBoxModel CmbCategoriaModel = new DefaultComboBoxModel();
			/*new DefaultComboBoxModel(
					new String[] { "Item One", "Item Two" });*/
		int i;
		for(i=0;i<vector.size();i++){
			CmbCategoriaModel.addElement(vector.elementAt(i).getNombre());
		}
		CmbCategoria = new JComboBox();
		getContentPane().add(CmbCategoria);
		CmbCategoria.setModel(CmbCategoriaModel);
		CmbCategoria.setBounds(166, 143, 135, 21);
		{
			LblCantidad = new JLabel();
			getContentPane().add(LblCantidad);
			LblCantidad.setBounds(321, 189, 83, 18);
			LblCantidad.setText("Cantidad");
		}
		{
			TxtCantidad = new JTextField();
			getContentPane().add(TxtCantidad);
			TxtCantidad.setBounds(383, 188, 107, 21);
		}
	}
	
	public void MensajeExito(){
		JOptionPane.showMessageDialog(rootPane,"Operacion Completada");
		this.dispose();
	}
	
	public void MensajeSalir(){
		JOptionPane.showMessageDialog(rootPane,"No se ha almacenado ningun producto");
		this.dispose();
	}
	
	public void AgregarCodigoProducto(int n){
		TxtCodigo.setText(String.valueOf(n));
	}
	
	public void AgregarComboIngrediente(Vector<Ingrediente> vector){
		DefaultComboBoxModel CmbIngredienteModel = new DefaultComboBoxModel();
		int i;
		for(i=0;i<vector.size();i++){
			CmbIngredienteModel.addElement(vector.elementAt(i).getNombre());
		}
			/*new DefaultComboBoxModel(
					new String[] { "Item One", "Item Two" });*/
		CmbIngrediente = new JComboBox();
		getContentPane().add(CmbIngrediente);
		CmbIngrediente.setModel(CmbIngredienteModel);
		CmbIngrediente.setBounds(168, 184, 135, 21);
	}
	
	public void AgregarColumnas(){
		Vector columnas = new Vector();
		columnas.add("Ingrediente");
		columnas.add("cantidad");
		Jtable1Model.addColumn(columnas);
		/*
*/	}
	
	public boolean Validar(){
		return !TxtCodigo.getText().equals("") && !TxtNombre.getText().equals("") && !TxtPrecio.getText().equals("");
	}
	
	
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				LblCodigo = new JLabel();
				getContentPane().add(LblCodigo);
				LblCodigo.setText("Codigo Producto");
				LblCodigo.setBounds(20, 27, 98, 14);
			}
			{
				TxtCodigo = new JTextField();
				getContentPane().add(TxtCodigo);
				TxtCodigo.setBounds(118, 24, 116, 20);
			}
			{
				LblNombre = new JLabel();
				getContentPane().add(LblNombre);
				LblNombre.setText("Nombre");
				LblNombre.setBounds(20, 67, 75, 14);
			}
			{
				TxtNombre = new JTextField();
				getContentPane().add(TxtNombre);
				TxtNombre.setBounds(118, 64, 116, 21);
			}
			{
				LblPrecio = new JLabel();
				getContentPane().add(LblPrecio);
				LblPrecio.setText("Precio");
				LblPrecio.setBounds(20, 107, 59, 14);
			}
			{
				TxtPrecio = new JTextField();
				getContentPane().add(TxtPrecio);
				TxtPrecio.setBounds(118, 104, 116, 21);
			}
			{
				LblCategoria = new JLabel();
				getContentPane().add(LblCategoria);
				LblCategoria.setText("Categoria Perteneciente");
				LblCategoria.setBounds(20, 146, 146, 14);
			}
			{
				/*ComboBoxModel CmbCategoriaModel = 
					new DefaultComboBoxModel(
							new String[] { "Item One", "Item Two" });
				CmbCategoria = new JComboBox();
				getContentPane().add(CmbCategoria);
				CmbCategoria.setModel(CmbCategoriaModel);
				CmbCategoria.setBounds(166, 143, 135, 21);*/
			}
			{
				LblIngrediente = new JLabel();
				getContentPane().add(LblIngrediente);
				LblIngrediente.setText("Seleccion Ingrediente");
				LblIngrediente.setBounds(20, 187, 140, 14);
			}
			{
				/*ComboBoxModel CmbIngredienteModel = 
					new DefaultComboBoxModel(
							new String[] { "Item One", "Item Two" });
				CmbIngrediente = new JComboBox();
				getContentPane().add(CmbIngrediente);
				CmbIngrediente.setModel(CmbIngredienteModel);
				CmbIngrediente.setBounds(168, 184, 135, 21);*/
			}
			{
				Jtable1Model= new DefaultTableModel(0,1);
				AgregarColumnas();
					/*new DefaultTableModel(
							new String[][] { { "One", "Two" }, { "Three", "Four" } },
							new String[] { "Column 1", "Column 2" });*/
				
				Jtable1 = new JTable();
				getContentPane().add(Jtable1);
				Jtable1.setModel(Jtable1Model);
				Jtable1.setBounds(20, 234, 440, 240);
			}
			{
				BtnGuardar = new JButton();
				getContentPane().add(BtnGuardar);
				BtnGuardar.setText("Guardar");
				BtnGuardar.setBounds(20, 486, 90, 21);
			}
			{
				BtnAgregar = new JButton();
				getContentPane().add(BtnAgregar);
				BtnAgregar.setText("Agregar");
				BtnAgregar.setBounds(494, 234, 80, 21);
			}
			{
				BtnEliminar = new JButton();
				getContentPane().add(BtnEliminar);
				BtnEliminar.setText("Eliminar");
				BtnEliminar.setBounds(494, 279, 80, 21);
			}
			pack();
			this.setSize(630, 562);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

}
